import configparser
import os
from AStock.ASSetting.path_def import settings_path
from AStock.ASUtil import create_mongo_client, create_async_mongo_client
from AStock.ASSetting.cons import CrawlSource

DEFAULT_DBNAME = os.getenv('DBNAME', 'quantaxis')
DEFAULT_MONGO_URI = os.getenv('MONGODB_URI', 'mongodb://127.0.0.1:27017')
CONFIG_FILE_PATH = os.path.join(settings_path, 'config.ini')


class ASSetting(object):

    def __init__(self, mongo_uri=None):
        if not os.path.exists(CONFIG_FILE_PATH):
            self._make_default_config()

        self._get_config()

        if mongo_uri:
            self._mongo_uri = mongo_uri

    def __repr__(self):
        return 'financial_source_cwbl={}\n' \
               'financial_source_zcfzb={}\n' \
               'financial_source_xjllb={}\n' \
               'financial_source_lrb={}\n' \
               'financial_source_notice_date={}\n' \
               'research_report_source={}\n' \
               'pe_percentage={}\n' \
               'financial_source_yjyg={}\n' \
               'financial_source_yjkb={}\n' \
               'stock_day_source={}'.format(
            self.financial_source_cwbl,
            self.financial_source_zcfzb,
            self.financial_source_xjllb,
            self.financial_source_lrb,
            self.financial_source_notice_date,
            self.research_report_source,
            self.pe_percentage_source,
            self.financial_source_yjyg,
            self.financial_source_yjkb,
            self.stock_day_source
        )

    def _get_config(self):
        config = configparser.ConfigParser()
        config.read(CONFIG_FILE_PATH)
        self._mongo_uri = config.get(
            'MONGODB', 'uri', fallback=DEFAULT_MONGO_URI)
        self._dbname = config.get(
            'MONGODB', 'dbname', fallback=DEFAULT_DBNAME)
        try:
            cwbl = config.get(
                'DATA_SOURCE', 'financial_cwbl', fallback=None)
            self._financial_source_cwbl = CrawlSource(cwbl)
        except ValueError:
            print('config error: DATA_SOURCE/financial_cwbl, use default: sina')
            self._financial_source_cwbl = CrawlSource.SINA

        try:
            zcfzb = config.get(
                'DATA_SOURCE', 'financial_zcfzb', fallback=None)
            self._financial_source_zcfzb = CrawlSource(zcfzb)
        except ValueError:
            print('config error: DATA_SOURCE/financial_zcfzb, use default: hexun')
            self._financial_source_zcfzb = CrawlSource.HEXUN

        try:
            lrb = config.get(
                'DATA_SOURCE', 'financial_lrb', fallback=None)
            self._financial_source_lrb = CrawlSource(lrb)
        except ValueError:
            print('config error: DATA_SOURCE/financial_lrb, use default: hexun')
            self._financial_source_lrb = CrawlSource.HEXUN

        try:
            xjllb = config.get(
                'DATA_SOURCE', 'financial_xjllb', fallback=None)
            self._financial_source_xjllb = CrawlSource(xjllb)
        except ValueError:
            print('config error: DATA_SOURCE/financial_xjllb, use default: hexun')
            self._financial_source_xjllb = CrawlSource.HEXUN

        try:
            notice_date = config.get(
                'DATA_SOURCE', 'financial_notice_date', fallback=None)
            self._financial_source_notice_date = CrawlSource(notice_date)
        except ValueError:
            print('config error: DATA_SOURCE/financial_notice_date, use default: eastmoney')
            self._financial_source_notice_date = CrawlSource.EASTMONEY

        try:
            research_report = config.get(
                'DATA_SOURCE', 'research_report', fallback=None)
            self._research_report_source = CrawlSource(research_report)
        except ValueError:
            print('config error: DATA_SOURCE/research_report, use default: eastmoney')
            self._research_report_source = CrawlSource.EASTMONEY

        try:
            pe_percentage = config.get(
                'DATA_SOURCE', 'pe_percentage', fallback=None)
            self._pe_percentage_source = CrawlSource(pe_percentage)
        except ValueError:
            print('config error: DATA_SOURCE/pe_percentage, use default: eastmoney')
            self._pe_percentage_source = CrawlSource.EASTMONEY

        try:
            yjyg = config.get(
                'DATA_SOURCE', 'financial_yjyg', fallback=None)
            self._financial_source_yjyg = CrawlSource(yjyg)
        except ValueError:
            print('config error: DATA_SOURCE/financial_yjyg, use default: eastmoney')
            self._financial_source_yjyg = CrawlSource.EASTMONEY

        try:
            yjkb = config.get(
                'DATA_SOURCE', 'financial_yjkb', fallback=None)
            self._financial_source_yjkb = CrawlSource(yjkb)
        except ValueError:
            print('config error: DATA_SOURCE/financial_yjkb, use default: eastmoney')
            self._financial_source_yjkb = CrawlSource.EASTMONEY

        try:
            stock_day = config.get(
                'DATA_SOURCE', 'stock_day', fallback=None)
            self._stock_day_source = CrawlSource(stock_day)
        except ValueError:
            print('config error: DATA_SOURCE/stock_day, use default: tencent')
            self._stock_day_source = CrawlSource.TENCENT

    @staticmethod
    def _make_default_config():
        config = configparser.ConfigParser()

        config.add_section('MONGODB')
        config.set('MONGODB', 'uri', DEFAULT_MONGO_URI)
        config.set('MONGODB', 'dbname', DEFAULT_DBNAME)

        config.add_section('DATA_SOURCE')
        config.set('DATA_SOURCE', 'financial_cwbl', str(CrawlSource.SINA.value))
        config.set('DATA_SOURCE', 'financial_zcfzb', str(CrawlSource.SINA.value))
        config.set('DATA_SOURCE', 'financial_lrb', str(CrawlSource.SINA.value))
        config.set('DATA_SOURCE', 'financial_xjllb', str(CrawlSource.SINA.value))
        config.set('DATA_SOURCE', 'financial_notice_date', str(CrawlSource.EASTMONEY.value))
        config.set('DATA_SOURCE', 'research_report', str(CrawlSource.EASTMONEY.value))
        config.set('DATA_SOURCE', 'pe_percentage', str(CrawlSource.EASTMONEY.value))
        config.set('DATA_SOURCE', 'financial_yjyg', str(CrawlSource.EASTMONEY.value))
        config.set('DATA_SOURCE', 'financial_yjkb', str(CrawlSource.EASTMONEY.value))
        config.set('DATA_SOURCE', 'stock_day', str(CrawlSource.TENCENT.value))

        with open(CONFIG_FILE_PATH, 'wt') as f:
            config.write(f)

    @property
    def client(self):
        return create_mongo_client(self._mongo_uri)

    @property
    def async_client(self):
        return create_async_mongo_client(self._mongo_uri)

    @property
    def database(self):
        return self.client[self._dbname]

    @property
    def async_database(self):
        return self.async_client[self._dbname]

    @property
    def financial_source_cwbl(self):
        return self._financial_source_cwbl

    @property
    def financial_source_zcfzb(self):
        return self._financial_source_zcfzb

    @property
    def financial_source_lrb(self):
        return self._financial_source_lrb

    @property
    def financial_source_xjllb(self):
        return self._financial_source_xjllb

    @property
    def financial_source_notice_date(self):
        return self._financial_source_notice_date

    @property
    def financial_source_yjyg(self):
        return self._financial_source_yjyg

    @property
    def financial_source_yjkb(self):
        return self._financial_source_yjkb

    @property
    def research_report_source(self):
        return self._research_report_source

    @property
    def pe_percentage_source(self):
        return self._pe_percentage_source

    @property
    def stock_day_source(self):
        return self._stock_day_source


settings = ASSetting()

